#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void printCapacity(const vector<int> &vec)
{
    cout << "size() = " << vec.size() << endl;
    cout << "capacity() = " << vec.capacity() << endl;
}

void test()
{
    vector<int> number = {1, 3, 5, 7, 9, 4, 6, 8, 2};
    display(number);
    printCapacity(number);

    cout << endl << "在vector的尾部进行插入与删除" << endl;
    number.push_back(100);
    number.push_back(200);
    display(number);
    printCapacity(number);

    cout << endl << "在vector的任意位置进行插入" << endl;
    vector<int>::iterator it = number.begin();
    it += 2;
    cout << "*it = " << *it << endl;
    number.insert(it, 300);
    display(number);
    cout << "*it = " << *it << endl;
    printCapacity(number);

    cout << endl;
    it = number.begin();
    it += 2;
    cout << "*it = " << *it << endl;
    //size() = m = 12, capacity() = n = 18, 新插入的元素个数t
    //1、t < n - m,此时不会发生扩容
    //2、n - m < t < m,此时会按照2 * size()进行扩容
    //3、n - m < t < n, m < t < n, 此时会按照m + t进行扩容
    //4、n - m < t , n < t, 此时会按照m + t进行扩容
    number.insert(it, 100, 100);
    display(number);
    cout << "*it = " << *it << endl;
    printCapacity(number);

    cout << endl;
    vector<int> vec = {11, 33, 55, 88, 22};
    it = number.begin();
    it += 2;
    cout << "*it = " << *it << endl;
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;
    printCapacity(number);
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

